﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml;
using System.Data;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Core;
using GSLMS.Services.Interface;

namespace GSLMS.WebAPP
{
    public class HelperCommon
    {
        protected static readonly ISystemConfig serviceSystemConfig = ServiceFactory.GetService<ISystemConfig>();


        public static readonly string AJAXFORMAT = "\"statusCode\":\"{0}\", \"message\":\"{1}\", \"navTabId\":\"{2}\", \"forwardUrl\":\"{3}\", \"callbackType\":\"{4}\"";
        private const string XML_PATH = @"~/Areas";
        private const string XML_FILE = "Role.xml";
        public static List<Module> GetModulesFromXML()
        {
            List<Module> resultTmp = new List<Module>();
            string directoryfullpath = HttpContext.Current.Server.MapPath(XML_PATH);
            var directorys = Directory.GetDirectories(directoryfullpath);            
            var files = FileManagement.GetAllFileName(directoryfullpath, XML_FILE);
            foreach (var item in files)
            {
                var modules = XmlUtil.Deserialize<List<Module>>(item as string);
                if (modules != null)
                {
                    foreach (var modele in modules)
                    {
                        if (resultTmp.Where(c => c.ID == modele.ID).Count() == 0)
                        {
                            resultTmp.Add(modele);
                        }
                    }
                }
            }


            return resultTmp;
        }
        static void InitProperties()
        {
            var configsCache = HttpContext.Current.Cache["SystemConfigAll"] as List<SystemConfig>;

            if (configsCache == null)
            {
                HttpContext.Current.Cache["SystemConfigAll"] = configsCache = serviceSystemConfig.GetAll();
            }
            SystemConfig config = null;
            int cvalue = 0;
            //HelperCommon obj = new HelperCommon();
            var entity = typeof(HelperCommon);
            string myValue = string.Empty;
            foreach (var Myproperty in entity.GetProperties())
            {
                config = configsCache.Where(c => c.Key == Myproperty.Name).FirstOrDefault();

                if (config != null)
                {
                    //int.TryParse(config.Value, out cvalue);
                    //item.SetValue(typeof(HelperCommon), cvalue, null);
                    myValue = config.Value;
                    if (Myproperty != null)
                    {
                        switch (Myproperty.PropertyType.ToString())
                        {
                            case "System.Boolean":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToBoolean(myValue.ToString()), null);
                                break;
                            case "System.Byte":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToByte(myValue.ToString()), null);
                                break;
                            case "System.Char":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToChar(myValue.ToString()), null);
                                break;
                            case "System.DateTime":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToDateTime(myValue.ToString()), null);
                                break;
                            case "System.Nullable`1[System.DateTime]":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToDateTime(myValue.ToString()), null);
                                break;
                            case "System.Decimal":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToDecimal(myValue.ToString()), null);
                                break;
                            case "System.Double":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToDouble(myValue.ToString()), null);
                                break;
                            case "System.Int16":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToInt16(myValue.ToString()), null);
                                break;
                            case "System.Int32":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToInt32(myValue.ToString()), null);
                                break;
                            case "System.Int64":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToInt64(myValue.ToString()), null);
                                break;
                            case "System.SByte":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToSByte(myValue.ToString()), null);
                                break;
                            case "System.Nullable`1[System.SByte]":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToSByte(myValue.ToString()), null);
                                break;
                            case "System.Single":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToSingle(myValue.ToString()), null);
                                break;
                            case "System.String":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, myValue.ToString(), null);
                                break;
                            case "System.UInt16":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToUInt16(myValue.ToString()), null);
                                break;
                            case "System.UInt32":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToUInt32(myValue.ToString()), null);
                                break;
                            case "System.UInt64":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToUInt64(myValue.ToString()), null);
                                break;
                            case "System.Guid":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, new Guid(myValue.ToString()), null);
                                break;
                            case "System.Byte[]":
                                if (myValue != null)
                                    Myproperty.SetValue(entity, Convert.ToByte(myValue.ToString()), null);
                                break;
                            default:
                                if (myValue != null)
                                    try
                                    {
                                        Myproperty.SetValue(entity, Convert.ToString(myValue.ToString()), null);
                                    }
                                    catch (Exception e) { new Loger().ExceptionHand(e); }
                                break;
                        }
                    }

                }
            }
        }
        public HelperCommon()
        {
            //InitProperties();
        }
        static HelperCommon()
        {
            //InitProperties();
        }

        /// <summary>
        /// 精彩回顾显示数目
        /// </summary>
        public static int RostrumIsTop { get; set; }

        /// <summary>
        /// 诸子名录条数
        /// </summary>
        public static int LecturerIsTop { get; set; }

        /// <summary>
        /// 诸子名录条数
        /// </summary>
        public static int TravelIsTop { get; set; }

        /// <summary>
        /// 国学教育页数
        /// </summary>
        public static int EducationPagesize { get; set; }

        /// <summary>
        /// 活动策划页数
        /// </summary>
        public static int PlanPageSize { get; set; }

        /// <summary>
        /// 汉古书书院页数
        /// </summary>
        public static int LibraryPageSize { get; set; }

        /// <summary>
        /// 积分兑换页数
        /// </summary>
        public static int ChargePageSize { get; set; }

        /// <summary>
        /// 最新动态 页数
        /// </summary>
        public static int NewsPageSize { get; set; }

        /// <summary>
        /// 精采回顾页数
        /// </summary>
        public static int RostrumPageSize { get; set; }

        /// <summary>
        /// 游学九州 列表页数
        /// </summary>
        public static int TravelPageSize { get; set; }

        /// <summary>
        /// VIP过期验证时间（分）
        /// </summary>
        public static int ValidateCodeDated { get; set; }
        /// <summary>
        /// 客服在线代码
        /// </summary>
        public static string OnlineSource { get; set; }

        /// <summary>
        /// 发件人邮件地址
        /// </summary>
        public static string SenderAddress { get; set; }

        /// <summary>
        /// 发件人名字
        /// </summary>
        public static string SenderName { get; set; }

        /// <summary>
        /// 发件人邮箱密码
        /// </summary>
        public static string SenderPwd { get; set; }

        /// <summary>
        /// SMTP服务地址
        /// </summary>
        public static string SMTPService { get; set; }

        public static string SelectDefaultValue = "--请选择--";

        ///// <summary>
        ///// 多语言实体
        ///// </summary>
        //public static List<MultiLang> MultiLangs
        //{
        //    get
        //    {
        //        List<MultiLang> multiLangs = HttpContext.Current.Cache["MultiLangs"] as List<MultiLang>;
        //        if (multiLangs == null)
        //        {
        //            IMultiLang serviceMultiLang = ServiceFactory.GetService<IMultiLang>();
        //            multiLangs = serviceMultiLang.GetAll();
        //            if (multiLangs == null || multiLangs.Count == 0)
        //            {
        //                multiLangs = new List<MultiLang>() { new MultiLang() { ID = 0, Title = "中文", LangType = 0, Url = string.Empty } };
        //            }

        //            HttpContext.Current.Cache["MultiLangs"] = multiLangs;
        //        }
        //        return multiLangs;
        //    }
        //    set
        //    {
        //        HttpContext.Current.Cache["MultiLangs"] = value;
        //    }
        //}
    }
}